package com.wanmait.deliverymangementsystem.usercontroller;


import com.wanmait.deliverymangementsystem.pojo.Orders;
import com.wanmait.deliverymangementsystem.pojo.Tasks;
import com.wanmait.deliverymangementsystem.pojo.UserAddress;
import com.wanmait.deliverymangementsystem.pojo.Users;
import com.wanmait.deliverymangementsystem.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("user")
public class UserTaskController {
    @Resource
    private UsersService usersService;
    @Resource
   private TaskService taskService;
    @Resource
    private CityService cityService;
    @Resource
    private OrdersService ordersService;
    @Resource
    private UserAddressService userAddressService;


    //创建任务
    @GetMapping("createTask")
    @ModelAttribute("userAddressList")
    public List<UserAddress> creatTask(HttpSession session){
            Users user =(Users)session.getAttribute("user");
            List<UserAddress> userAddressList=userAddressService.showUserAddress(user);
        return userAddressList;
    };
    @PostMapping("createTask")
    public String creatTask(Tasks tasks, HttpSession session,Integer cityId){
        tasks.setUser((Users) session.getAttribute("user"));
        taskService.createTask(tasks);
        tasks.setId(taskService.findLastID());
        Orders orders =new Orders();
        orders.setTask(tasks);
       Users user= (Users) session.getAttribute("user");
       double money=user.getWallet()-tasks.getReward().doubleValue();
        DecimalFormat df =new DecimalFormat("#.00");
        money=Double.parseDouble(df.format(money));
       user.setWallet(money);
        orders.setUsers(user);
        orders.setOrderNumber(UUID.randomUUID().toString());
        orders.setStatus(0);
        orders.setCity(cityService.findCityByID(cityId));
        ordersService.create(orders);
        usersService.charge(user);
        session.setAttribute("user",user);
        return "redirect:/user/waitAcceptOrderList";
    }


}
